export function getExpiredStorage(storage, expiresTime) {
    return {
        getItem(key) {
            const itemStr = storage.getItem(key)
            if (!itemStr) {
                return null
            }
            // 读取的时候是字符串，需要转换为对象  
            const item = JSON.parse(itemStr)
            const now = new Date()
            if (now.getTime() > item.expiry) {
                storage.removeItem(key)
                return null
            }
            // pinia 的持久化插件要求返回JSON字符串  
            return JSON.stringify(item.value)
        },
        setItem(key, value) {
            const now = new Date()
            const item = {
                value: JSON.parse(value), // value是JSON字符串，需要转换为对象  
                expiry: now.getTime() + Number(expiresTime) * 1000,
            }
            storage.setItem(key, JSON.stringify(item)) // 存储又要转为JSON字符串  
        },
        removeItem(key) {
            storage.removeItem(key)
        },
        clear() {
            Object.keys(storage).forEach((key) => {
                storage.removeItem(key)
            })
        },
        key(index) {
            const keys = Object.keys(storage)
            return keys[index] || null
        },
        length: Object.keys(storage).length,
    }
  }